package com.quectel.mobile.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.SwaggerUiConfigParameters;
import org.springdoc.core.SwaggerUiConfigProperties;
import org.springdoc.core.SwaggerUiOAuthProperties;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.webmvc.ui.SwaggerIndexPageTransformer;
import org.springdoc.webmvc.ui.SwaggerWelcomeCommon;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.resource.ResourceTransformerChain;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;

/**
 * 在线文档
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-12-29 10:07:00
 */
@Configuration
@ConditionalOnProperty(name = {SPRINGDOC_SWAGGER_UI_ENABLED, SPRINGDOC_ENABLED}, havingValue = "true")
public class OpenApiConfig extends SwaggerIndexPageTransformer {
    private static final String PATH_SWAGGER_FLAG = "swagger-ui";

    public OpenApiConfig(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerWelcomeCommon swaggerWelcomeCommon, ObjectMapperProvider objectMapperProvider) {
        super(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, swaggerWelcomeCommon, objectMapperProvider);
    }

    @Override
    public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
        String requestURI = request.getRequestURI();
        if (!StringUtils.contains(requestURI, PATH_SWAGGER_FLAG)) {
            return super.transform(request, resource, transformerChain);
        }
        String contextPath = request.getRequestURI().substring(0, request.getRequestURI().indexOf(PATH_SWAGGER_FLAG));
        if (!contextPath.endsWith("/")) {
            // 结尾没有携带/需要补充
            contextPath += "/";
        }
        // 强制修改配置地址
        this.swaggerUiConfigParameters.setConfigUrl(contextPath + "v3/api-docs/swagger-config");
        // 强制修改文档地址
        this.swaggerUiConfigParameters.setUrl(contextPath + "v3/api-docs");
        return super.transform(request, resource, transformerChain);
    }

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .components(
                        new Components()
                                .addSecuritySchemes("MyTokenScheme", new SecurityScheme().type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER).name("token"))
                )
                .addSecurityItem(new SecurityRequirement().addList("MyTokenScheme"))
                .info(new Info()
                        .title("SpringBoot Online Document")
                        .version("OpenAPI 3.0")
                );
    }


}
